package dao

import (
	"context"
	dbutil "dist/db"
	"dist/model"
	"dist/utils"
	"fmt"
	"time"
)

func InsertPluginUsage(ts time.Time, pluginName string) bool {
	conn, _ := dbutil.GetConnection()
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	_, err := conn.ExecContext(ctx, "INSERT INTO business_use_day(ts, business_name) VALUES($1,$2)", ts, pluginName)
	defer conn.Close()
	if err != nil {
		utils.Error.Printf("business_use_day table insert failed, pluginName is %v, insert error is %v", pluginName, err)
		return false
	}
	return true
}

func IsTsInBusinessUseTotalTbl(ts time.Time) bool {
	conn, _ := dbutil.GetConnection()
	defer conn.Close()
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	rows, err := conn.QueryContext(ctx, "SELECT COUNT(*) FROM business_use_total WHERE ts=$1", ts)
	if err != nil {
		utils.Error.Printf("isTsInBusinessUseTotalTbl: select min ts, error: %v", err)
		return true
	}
	defer rows.Close()
	if rows.Next() {
		var cnt int
		err = rows.Scan(&cnt)
		if err != nil {
			utils.Error.Printf("isTsInBusinessUseTotalTbl: scan max ts, error: %v", err)
			return true
		}
		if cnt == 0 {
			return false
		} else {
			return true
		}
	} else {
		return true
	}
}

func InsertBusinessUseTotal(ts time.Time) {
	conn, _ := dbutil.GetConnection()
	defer conn.Close()
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	_, err := conn.ExecContext(ctx, "INSERT INTO business_use_total SELECT MIN(ts::DATE), business_name, count(*) AS cnt FROM business_use_day WHERE (ts::DATE)=$1 GROUP BY business_name", ts)
	if err != nil {
		utils.Error.Printf("business_use_total table insert failed, ts is %v, insert error is %v", ts, err)
		return
	}
}

func DeleteBusinessUseDay(ts time.Time) {
	conn, _ := dbutil.GetConnection()
	defer conn.Close()
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	_, err := conn.ExecContext(ctx, "DELETE FROM business_use_day WHERE ts<=$1", ts)
	if err != nil {
		utils.Error.Printf("deleteBusinessUseDay delete failed, ts is %v, delete error is %v", ts, err)
		return
	}
}

func GetBusinessUse(startTime time.Time, endTime time.Time, interval string) []model.BusinessUseData {
	var businessUseDataList []model.BusinessUseData
	conn, _ := dbutil.GetConnection()
	defer conn.Close()
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	querySql := fmt.Sprintf(utils.GET_BUSINESS_USE_SQL, interval)
	rows, err := conn.QueryContext(ctx, querySql, startTime, endTime)
	if err != nil {
		utils.Error.Printf("sqlStr is %s, error is %v", querySql, err)
		return nil
	}
	defer rows.Close()
	for rows.Next() {
		var businessUseData model.BusinessUseData
		err = rows.Scan(&businessUseData.Date, &businessUseData.Type, &businessUseData.Value)
		if err != nil {
			utils.Error.Printf("%v", err)
			return nil
		}
		businessUseDataList = append(businessUseDataList, businessUseData)
	}
	return businessUseDataList
}
